package com.zhisheng.sql.blink.stream.example.test;

import com.zhisheng.sql.blink.stream.example.WinAlert;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;

public class SimpleEmailSink extends RichSinkFunction<WinAlert.Alert> {

    // 邮件配置（根据你的邮箱服务商修改）
    private static final String smtpHost = "smtp.gmail.com"; // 例如Gmail
    private static final int smtpPort = 587;
    private static final String username = "hhhuawuque@gmail.com";
    private  static final String password = "jwiuqabvcukghqgo "; // 需开启两步验证
    private  static final String from = "hhhuawuque@gmail.com";
    private static final String to = "984358499@qq.com";

    private Session mailSession;

    @Override
    public void open(Configuration parameters) throws Exception {
        // 初始化邮件会话
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);       // 使用配置的host
        props.put("mail.smtp.port", smtpPort);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true"); // TLS

        mailSession = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
    }

    @Override
    public void invoke(WinAlert.Alert alert, Context context) {
        try {
            // 创建邮件
            MimeMessage message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("玩家赢钱告警: " + alert.playerId);
            
            String emailText = String.format(
                "玩家ID: %d\n累计赢钱: $%.2f\n时间: %s",
                alert.playerId,
                alert.totalWin
            );
            message.setText(emailText);

            // 发送
            Transport.send(message);
            System.out.println("邮件已发送至: " + to);
            
        } catch (MessagingException e) {
            System.err.println("邮件发送失败: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        // 初始化邮件会话
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);       // 使用配置的host
        props.put("mail.smtp.port", smtpPort);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.ssl.protocols", "TLSv1.2 TLSv1.3"); // 强制使用高版本
        props.put("mail.smtp.ssl.ciphersuites",
                "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256");
        // TLS

        Session mailSession = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        mailSession.setDebug(true);

        try {
            // 创建邮件
            MimeMessage message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("玩家赢钱告警: " + 111);

            String emailText = String.format(
                    "玩家ID: %d\n累计赢钱: $%.2f\n时间: %s",
                    111,
                    1000.00,"hhhhh"
            );
            message.setText(emailText);

            // 发送
            Transport.send(message);
            System.out.println("邮件已发送至: " + to);

        } catch (MessagingException e) {
            System.err.println("邮件发送失败: " + e.getMessage());
        }
    }
}